package com.hanamaki.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class DruidUtil {
    /* 连接信息 */
    static Properties properties;
    static Connection connection;
    static DataSource dataSource;
    /* 加载配置文件中的信息 */
    static {
        try {
            InputStream resourceAsStream = DruidUtil.class.getClassLoader().getResourceAsStream("db.dbcp.properties");
            properties = new Properties();
            properties.load(resourceAsStream);
            /* 使用德鲁伊的方式 ；来 创建数据源 */
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
        }
    }

    /**
     * 连接数据库的方法
     * @return
     */
    public static Connection getCon() {
        try {
            connection = dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

    public static void release(Connection connection, Statement statement, ResultSet resultSet, PreparedStatement preparedStatement) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 编写一个 读取数据库中数据的方法
     * @param tableName :表名
     * @param param:查询的字段名
     * @return
     */
    public static Object[][] getSqlContent(String tableName, String param[]) {
        /* 定义一个 二维数组 */
        Object[][] result = null;
        Connection con = DruidUtil.getCon();
        /* 通过 可变长的字符串来封装 sql语句 */
        StringBuilder sql = new StringBuilder();
        sql.append("select ");
        for (int i = 0; i < param.length; i++) {
            sql.append(param[i]);
            if (param.length - 1 != i) {
                sql.append(",");
            }
        }
        sql.append(" from ").append(tableName);
        System.out.println(sql);
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Object[]> list = null;
        try {
            preparedStatement = con.prepareStatement(sql.toString());
            resultSet = preparedStatement.executeQuery();
            /* 通过  resultSet获得 表的 源数据 */
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            /* 定义一个集合 List */
            list = new ArrayList<>();
            System.out.println(columnCount);
            while (resultSet.next()) {
                /* 定义一个 一维数组 来保存每一次取出的 一行的数据 */
                Object[] rows = new Object[columnCount];
                /* 因为在循环取值的时候 是一行一行取值的 所以我们可以通过 遍历行中的所有的列来取出数据 */
                for (int i = 0; i < columnCount; i++) {
                    rows[i] = resultSet.getObject(i + 1);
                }
                list.add(rows);
            }
            result = new Object[list.size()][];
            for (int i = 0; i < list.size(); i++) {
                result[i] = list.get(i);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.release(con, null, resultSet, preparedStatement);
        }
        return result;
    }
}
